package com.myapp.tools.dbbuilder;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.myapp.tools.dbbuilder.base.FieldBean;
import com.myapp.tools.dbbuilder.base.TableAllBean;
import com.myapp.tools.dbbuilder.base.TableBean;
import com.myapp.tools.util.ExcelUtils;

public class DBParse {

	private DBBuilder dbBuilder = new DBBuilder();

	// 日志
	private Logger log = Logger.getLogger(DBParse.class);

	public void parseAllTable(String path) {
		Object[][] data = null;
		String sheetName = null;
		try {
			InputStream is = new FileInputStream(path);
			HSSFWorkbook workbook = new HSSFWorkbook(is);
			int count = workbook.getNumberOfSheets();
			for (int i = 0; i < count; i++) {
				sheetName = workbook.getSheetName(i);
				if ("表一览".equals(sheetName)) {
					data = ExcelUtils.loadConfig(path, sheetName);
					if (data.length > 0) {
						// 数据库配置表
						this.parseAllTable(path, data);
					}
				}
			}
		} catch (IOException e) {
			log.error(String.format("Read Excel error - File[%s]", path), e);
			System.exit(1);
		} catch (Exception e) {
			log.error(String.format("Parse error - File[%s] - SheetName[%s]",
					path, sheetName), e);
			System.exit(1);
		}
	}

	private void parseAllTable(String path, Object[][] data) {
		TableAllBean tmp;
		for (int i = 1; i < data.length; i++) {
			tmp = new TableAllBean();

			tmp.setEn(String.valueOf(data[i][1]));

			tmp.setCn(String.valueOf(data[i][2]));

			if (!"".equals(data[i][3]) && null == data[i][3]) {
				tmp.setFieldCount(Integer.parseInt(String.valueOf(data[i][3])));
			}

			if ("1".equals(data[i][4])) {
				tmp.setCache(true);
			} else {
				tmp.setCache(false);
			}

			if ("1".equals(data[i][5])) {
				tmp.setBuild(true);
			} else {
				tmp.setBuild(false);
			}
			tmp.setRemark(String.valueOf(data[i][6]));

			this.parseTable(path, tmp);
		}
	}

	private void parseTable(String path, TableAllBean table) {
		String sheetName = table.getEn();
		try {
			// 读取数据
			Object[][] data = ExcelUtils.loadConfig(path, sheetName);
			// 构建一张表的数据
			this.parseOneTable(sheetName, data, table.getRemark());
		} catch (IOException e) {
			log.error(String.format("Read Excel error - File[%s]", path), e);
			System.exit(1);
		} catch (Exception e) {
			log.error(String.format("Parse error - File[%s] - SheetName[%s]",
					path, sheetName), e);
			System.exit(1);
		}
	}

	private void parseOneTable(String name, Object[][] data, String remark) {
		List<FieldBean> fields = new ArrayList<FieldBean>();
		FieldBean field = new FieldBean();
		for (int i = 4; i < data.length; i++) {
			field = new FieldBean();
			field.setEn(String.valueOf(data[i][1]));
			field.setCn(String.valueOf(data[i][2]));
			field.setType(String.valueOf(data[i][3]));
			field.setLength(Integer.parseInt(String.valueOf(data[i][4])));
			field.setDefaultData(String.valueOf(data[i][5]));
			if ("1".equals(data[i][6])) {
				field.setCanNull(true);
			} else {
				field.setCanNull(false);
			}
			field.setRemark(String.valueOf(data[i][7]));
			fields.add(field);
		}

		// 构建数据
		TableBean tableBean = new TableBean();
		tableBean.setFields(fields);

		// 设置名字
		tableBean.setName(name);

		// 设置index
		if (data[2][1] != null && !"".equals(data[2][1])) {
			tableBean.setIndexs(String.valueOf(data[2][1]));
		}

		// 设置主键
		if (data[0][1] != null && !"".equals(data[0][1])) {
			tableBean.setPrimayKey(String.valueOf(data[0][1]));
		} else {
			tableBean.setPrimayKey("");
		}

		// 设置表注释
		tableBean.setRemark(remark);

		// 构建表
		dbBuilder.builderDB(tableBean);
	}
}
